package org.study.dsm.ds.config;

import lombok.*;
import org.apache.commons.compress.utils.Lists;

import java.io.Serializable;
import java.util.List;

/**
 * 数据库类型枚举
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年08月30日
 */
@Getter
@AllArgsConstructor
public enum DatasourceTypeEnum {
    H2(1, "h2:mem", "org.h2.Driver", "H2内嵌式数据库", "jdbc:h2:mem:${db_name!}"),
    SQLITE(2, "sqlite", "org.sqlite.JDBC", "SQLite数据库", "jdbc:sqlite:/path/${db_name!}.db"),
    MYSQL(3, "mysql", "com.mysql.cj.jdbc.Driver", "MySQL数据库", "jdbc:mysql://${host!}:${port!}/${db_name!}?useUnicode=true&characterEncoding=UTF8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"),
    MARIADB(4, "mariadb", "org.mariadb.jdbc.Driver", "MariaDB数据库", "jdbc:mariadb://${host!}:${port!}/${db_name!}?useUnicode=true&characterEncoding=UTF8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"),
    POSTGRESQL(5, "postgresql", "org.postgresql.Driver", "PostgreSQL数据库", "jdbc:postgresql://${host!}:${port!}/${db_name!}"),
    TDENGINE(6, "TAOS-RS", "com.taosdata.jdbc.rs.RestfulDriver", "TDengine时序数据库", "jdbc:TAOS-RS://${host!}:${port!}/${db_name!}"),
    CLICKHOUSE(7, "clickhouse", "com.clickhouse.jdbc.ClickHouseDriver", "ClickHouse数据库", "jdbc:clickhouse://${host!}:${port!}/${db_name!}"),
    DM(8, "dm", "dm.jdbc.driver.DmDriver", "达梦数据库", "jdbc:dm://${host!}:${port!}/${db_name!}"),
    XUGU_RS(9, "xugu", "com.xugu.cloudjdbc.Driver", "虚谷数据库", "jdbc:xugu://${host!}:${port!}/${db_name!}"),
    XUGU_TS(10, "XGTS-RS", "com.xugutsdb.jdbc.rs.RestfulDriver", "虚谷澜沧时序数据库", "jdbc:XGTS-RS://${host!}:${port!}/${db_name!}"),
    DEFAULT(99999, "暂不支持该数据库", "暂不支持该数据库", "暂不支持该数据库", "暂不支持该数据库");

    private Integer typeCode;
    private String subProtocol;
    private String driverClassName;
    private String description;
    private String urlPattern;

    public static DatasourceTypeEnum getDsTypeEnum(Integer typeCode) {
        for (DatasourceTypeEnum dsType : values()) {
            if (dsType.getTypeCode().equals(typeCode)) {
                return dsType;
            }
        }
        throw new RuntimeException("暂不支持该数据库");
    }

    public static String getDriverClassName(Integer typeCode) {
        for (DatasourceTypeEnum dsType : values()) {
            if (dsType.getTypeCode().equals(typeCode)) {
                return dsType.getDriverClassName();
            }
        }
        throw new RuntimeException("暂不支持该数据库");
    }

    public static String getUrlPattern(Integer typeCode) {
        for (DatasourceTypeEnum dsType : values()) {
            if (dsType.getTypeCode().equals(typeCode)) {
                return dsType.getUrlPattern();
            }
        }
        throw new RuntimeException("暂不支持该数据库");
    }

    public static List<DatasourceTypeVO> getAllSupportDb() {
        List<DatasourceTypeVO> list = Lists.newArrayList();
        for (DatasourceTypeEnum dsType : values()) {
            if (DatasourceTypeEnum.DEFAULT != dsType) {
                DatasourceTypeVO datasourceTypeVO = new DatasourceTypeVO();
                datasourceTypeVO.setTypeCode(dsType.getTypeCode());
                datasourceTypeVO.setSubProtocol(dsType.getSubProtocol());
                datasourceTypeVO.setDriverClassName(dsType.getDriverClassName());
                datasourceTypeVO.setDescription(dsType.getDescription());
                datasourceTypeVO.setUrlPattern(dsType.getUrlPattern());
                list.add(datasourceTypeVO);
            }
        }
        return list;
    }

    @Data
    public static class DatasourceTypeVO implements Serializable {
        private static final long serialVersionUID = 1L;

        private Integer typeCode;
        private String subProtocol;
        private String driverClassName;
        private String description;
        private String urlPattern;
    }
}
